package com.wanmait.community.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.entity.Visitor;
import com.wanmait.community.service.VisitorService;
import com.wanmait.community.util.AjaxResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("visitor")
public class VisitorController {
    @Resource
    private VisitorService visitorService;
    @PostMapping("insert")
    public AjaxResult insert(@RequestBody Visitor visitor) {
        visitorService.insertVisitor(visitor);
        return AjaxResult.success("添加成功");
    }

    @PostMapping("pageInfo")
    public AjaxResult pageInfo(@RequestBody Visitor visitor,Integer pageNum) {
        Integer pageSize=8;
        PageInfo<Visitor> pageInfo = visitorService.handlelist(visitor,pageNum,pageSize);
        return AjaxResult.success().put("pageInfo", pageInfo);
    }

    @DeleteMapping("{id}")
    public AjaxResult update(@PathVariable Integer id) {
        visitorService.deleteVisitor(id);
        return AjaxResult.success();
    }

    @GetMapping("{id}")
    public AjaxResult select(@PathVariable Integer id) {
        Visitor visitor =  visitorService.selectVisitor(id);
        return AjaxResult.success().put("visitor", visitor);
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody Visitor visitor) {
        visitorService.updateVisitor(visitor);
        return AjaxResult.success();
    }
}
